#include <QApplication>
#include "mainwindow.h"

#include <QThreadPool>
#include <qtsingleapplication.h>

#include "proxystyle.h"


int main(int argc, char *argv[])
{
	srand((unsigned)time(NULL));

	QApplication::setGraphicsSystem(QLatin1String("raster"));

	QCoreApplication::setOrganizationName("Fundamental");
	QCoreApplication::setApplicationName("Fundamental");
	QSettings settings;
	Q_UNUSED(settings);

	QtSingleApplication a("fundamental", argc, argv);
	if (a.isRunning()) {
		argc--, argv++;
		while (argc > 0) {
			QString arg = argv[0];
			argc--, argv++;

			if (QFile(arg).exists() && arg.endsWith(".pdf", Qt::CaseInsensitive)) {
				a.sendMessage(QFileInfo(arg).absoluteFilePath());
			}
		}
		exit(0);
	}

	QThreadPool::globalInstance()->setMaxThreadCount(4);

	a.setStyle(returnSubclassStyleForBaseStyle(a.style()));
	MainWindow w;
	a.setActivationWindow(&w);
	w.showMaximized();

	argc--, argv++;
	while (argc > 0) {
		QString arg = argv[0];
		argc--, argv++;

		if (QFile(arg).exists() && arg.endsWith(".pdf", Qt::CaseInsensitive)) {
			w.openExternalFile(QFileInfo(arg).absoluteFilePath());
		}
	}

	QObject::connect(&a, SIGNAL(messageReceived(const QString&)),
			 &w, SLOT(openExternalFile(QString)));
	
	return a.exec();
}
